using System;

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Updating;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.Security;

namespace Liekhus.TimeTracker.Module
{
    public class Updater : ModuleUpdater
    {
        public Updater(ObjectSpace objectSpace, Version currentDBVersion) : base(objectSpace, currentDBVersion) { }
 
        public override void UpdateDatabaseAfterUpdateSchema()
        {
            base.UpdateDatabaseAfterUpdateSchema();

            
#if EASYTEST
            User user = ObjectSpace.FindObject<User>(CriteriaOperator.Parse("UserName == 'Tim'"));
            Role role = ObjectSpace.FindObject<Role>(CriteriaOperator.Parse("Name == 'Default'"));

            if (role == null)
            {
                role = ObjectSpace.CreateObject<Role>();
                role.Name = "Default";
                role.AddPermission(new ObjectAccessPermission(typeof(object), ObjectAccess.AllAccess, ObjectAccessModifier.Allow));
                role.Save();
            }

            if (user == null)
            {
                user = ObjectSpace.CreateObject<User>();
                user.UserName = "Tim";
                user.FirstName = "Tim";
                user.LastName = "Time Tracker";
                user.Roles.Add(role);
                user.Save();

                user.SetPassword("timpassword");
                user.Save();
            }

            user = ObjectSpace.FindObject<User>(CriteriaOperator.Parse("UserName == 'Mark'"));
            if (user == null)
            {
                user = ObjectSpace.CreateObject<User>();
                user.UserName = "Mark";
                user.FirstName = "Mark";
                user.LastName = "Manager";
                user.Roles.Add(role);
                user.Save();

                user.SetPassword("markpassword");
                user.Save();
            }

            user = ObjectSpace.FindObject<User>(CriteriaOperator.Parse("UserName == 'Adam'"));
            if (user == null)
            {
                user = ObjectSpace.CreateObject<User>();
                user.UserName = "Adam";
                user.FirstName = "Adam";
                user.LastName = "Administrator";
                user.Roles.Add(role);
                user.Save();

                user.SetPassword("adampassword");
                user.Save();
            }
#endif

            TaskStatus status = ObjectSpace.FindObject<TaskStatus>(CriteriaOperator.Parse("Code == 'P'"));
            if (status == null)
            {
                status = ObjectSpace.CreateObject<TaskStatus>();
                status.Code = "P";
                status.Name = "Pending";
                status.Save();
            }

            status = ObjectSpace.FindObject<TaskStatus>(CriteriaOperator.Parse("Code == 'F'"));
            if (status == null)
            {
                status = ObjectSpace.CreateObject<TaskStatus>();
                status.Code = "F";
                status.Name = "Finished";
                status.Save();
            }

            status = ObjectSpace.FindObject<TaskStatus>(CriteriaOperator.Parse("Code == 'L'"));
            if (status == null)
            {
                status = ObjectSpace.CreateObject<TaskStatus>();
                status.Code = "L";
                status.Name = "Later";
                status.Save();
            }

            CoveyQuandrant quadrant = ObjectSpace.FindObject<CoveyQuandrant>(CriteriaOperator.Parse("Code == '1'"));
            if (quadrant == null)
            {
                quadrant = ObjectSpace.CreateObject<CoveyQuandrant>();
                quadrant.Code = "1";
                quadrant.Name = "Important/Urgent";
                quadrant.Save();
            }

            quadrant = ObjectSpace.FindObject<CoveyQuandrant>(CriteriaOperator.Parse("Code == '2'"));
            if (quadrant == null)
            {
                quadrant = ObjectSpace.CreateObject<CoveyQuandrant>();
                quadrant.Code = "2";
                quadrant.Name = "Important/Not Urgent";
                quadrant.Save();
            }

            quadrant = ObjectSpace.FindObject<CoveyQuandrant>(CriteriaOperator.Parse("Code == '3'"));
            if (quadrant == null)
            {
                quadrant = ObjectSpace.CreateObject<CoveyQuandrant>();
                quadrant.Code = "3";
                quadrant.Name = "Not Important/Urgent";
                quadrant.Save();
            }

            quadrant = ObjectSpace.FindObject<CoveyQuandrant>(CriteriaOperator.Parse("Code == '4'"));
            if (quadrant == null)
            {
                quadrant = ObjectSpace.CreateObject<CoveyQuandrant>();
                quadrant.Code = "4";
                quadrant.Name = "Not Important/Not Urgent";
                quadrant.Save();
            }
        }

    }
}
